package com.turing.post.tuxedo.service;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.transaction.util.MrchBusiness;
import com.turing.post.transaction.util.MrchTransParam;
import com.turing.post.tuxedo.util.IPaidCancel;
import com.turing.post.tuxedo.util.LocalService;
import com.turing.post.tuxedo.util.LocalServiceImpl;
import com.turing.post.tuxedo.util.OPaidCancel;
import com.turing.post.tuxedo.util.PCPSService;
import com.turing.post.tuxedo.util.TransRequest;
import com.turing.post.tuxedo.util.TransResponse;
import com.turing.post.tuxedo.util.TuxedoRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

@Stateless(mappedName = "PCPS.SVC9020")
@Remote( { LocalService.class, PCPSService.class })
@Local
public class PaidCancel extends LocalServiceImpl implements LocalService,
		PCPSService {

	@EJB(mappedName = "transaction.impl.MrchBusinessImpl")
	private MrchBusiness mrchBusinessImpl;

	/**
	 * generate IPaidCancel
	 * 
	 * @return IPaidCancel
	 */
	public IPaidCancel makeRequestObject() {
		return new IPaidCancel();
	}

	/**
	 * PaidCancel 数据检查
	 * 
	 * @param req
	 * @param ack
	 */
	protected void preService(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {
	}

	/**
	 * PaidCancel service entry 预付卡支付撤销的业务逻辑
	 * 
	 * @param req
	 * @param ack
	 */
	protected void Service(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {

		IPaidCancel itr = (IPaidCancel) req;
		OPaidCancel otr = (OPaidCancel) ack;

		// 设置基本参数
		MrchTransParam imtp = setMrchTransParam(req);

		// 原交易主机流水号
		imtp.cancelTranSeqNo = itr.origAckSeqNo;
		// 原交易请求流水号
		imtp.cancelMrchReqSeqNo = itr.origReqSeqNo;
		// 原交易日期
		imtp.cancelTranDate = itr.origTranDate;

		// 交易摘要(可选)
		imtp.mrchAbstract = itr.reason;

		// 代理操作员号
		imtp.OperId = null;

		// 合计发生额
		imtp.totalAmount = itr.totalAmount;

		// 验证撤销时卡密码
		if (!PCPSUtil.isEmpty(itr.cardNo))
			mrchBusinessImpl.VerifyPaidCancel(imtp, itr.cardNo, itr.passwd);

		// 执行交易
		MrchTransParam omtp = mrchBusinessImpl.PaidCancel(imtp);

		// 主机流水号
		otr.ackSeqNo = omtp.tranSeqNo;
		otr.tranTime = itr.curSysDateTime;
		otr.respCode = TuxedoRespCode.RC_SUCCESS;
	}

	/**
	 * PaidCancel 服务入口
	 * 
	 * @param req
	 * @return TransResponse
	 */
	public TransResponse Service(TransRequest req)
			throws PCPSApplicationException {
		// 按普通方法执行服务
		return CommonService(req, new OPaidCancel());
	}
}